#pragma once
#include <sys/epoll.h>
#include <iostream>
#include <unistd.h>
using std::cout;
using std::endl;

class Epoll
{
    static const int _size = 128;
private:
    int _epfd;
    int _timeout{3000};

    Epoll(const Epoll&)=delete;
    Epoll& operator=(const Epoll&) =delete;
public:
    Epoll()
    {
        _epfd = epoll_create(_size);
        if(_epfd == -1)cout << "epoll create error "<< endl;
        cout << "epoll create success "<< _epfd << endl;
    }

    ~Epoll()
    {
        if(_epfd > 0)close(_epfd);
    }

public:
    int EpollCtl(int op, int fd, uint32_t event)
    {
        int n = 0;
        if(op == EPOLL_CTL_DEL)
        {
            n = epoll_ctl(_epfd, op, fd, nullptr);
            if(n != 0)cout << "epoll_ctl error " << endl;
        }
        else
        {
            //EPOLL_CTL_ADD or EPOLL_CTL_MOD
            struct epoll_event ev;
            ev.events = event;
            ev.data.fd = fd;
            n = epoll_ctl(_epfd, op, fd, &ev);
            if(n != 0)cout << "epoll_ctl error " << endl;
        }
        return n;
    }


    int EpollWait(struct epoll_event revents[], int num)
    {
        int n = epoll_wait(_epfd, revents, num, /*_timeout*/-1);
        return n;
    }
};


